//
//  BusinessViewController.m
//  HAODE
//
//  Created by panquanReabam.com on 15/9/8.
//  Copyright (c) 2015年 reabam.com. All rights reserved.
//


#import "ActivityDetailViewController.h"
#import "RDVTabBarController.h"
#import "RDVTabBarItem.h"
#import "CycleScrollView.h"
#import "HttpRequest.h"
#import "UIButton+WebCache.h"
#import "TheLoginViewController.h"
#import "ACETelPrompt.h"
#import <MapKit/MapKit.h>
#import "MapViewControllers.h"
#import "UMSocial.h"
#import "ActivityDetailView.h"
#import "ActivityDetailModel.h"
#import "ShareView.h"
#import "NewMediaPromotionViewController.h"
@interface ActivityDetailViewController ()<CLLocationManagerDelegate,UIGestureRecognizerDelegate,UINavigationControllerDelegate,UIScrollViewDelegate,UMSocialUIDelegate,ActivityDetailViewDelegate,ShareViewDelegate>
@property (nonatomic, unsafe_unretained) NSInteger currentIndex;
@property(nonatomic ,strong)NSDictionary*myDic;
@property(nonatomic,strong)  NSString*shareUrl;
@property(nonatomic,strong)  NSString*shuoming;
@property(nonatomic,strong)  NSString*content;
@property(nonatomic,strong)  NSString*address;
@property(nonatomic,strong)  NSString*endDate;

@end

@implementation ActivityDetailViewController
{
    
    CycleScrollView *mainScorllView;
    UIScrollView*mainScroll;
    UIScrollView*mainScroll1;
    NSDate* time;
    UILabel *timeLabel;
    UIView*shareview;
    UIButton* cancel;
    UIScrollView*shareScroll;
    NSDictionary*Dict;
    ActivityDetailView*act;
    ShareView*SV;
    UIView*shareblackview;
   }
@synthesize alt = alt;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"";
        
        
    }
    return self;
}



-(void)BackAction:(UIButton *)sender{
    
    
    [self.navigationController popViewControllerAnimated:YES];
    
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBarHidden=NO;
    [self setNavi];

    
}

- (void)viewDidLoad
{
    
    [super viewDidLoad];
    
     act = [[ActivityDetailView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth,ScreenHeight)];
    act.mDelegate=self;
     [self .view addSubview:act];
    
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
   
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:self.actId forKey:@"actID"];

    
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    [HttpRequest requestData:@"Business/Act/Detail" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                
                Dict=severDataDict.dataDict;
                ActivityDetailModel *dataModel=[ActivityDetailModel dataModelWithDict:Dict];
                act.model=dataModel;
        
            }else{
                [Global hideProgressHUDInternal:self.view];
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
    

    
    
}

-(void)closeKeyBoad
{
    [self.window makeKeyWindow];
    self.window.hidden=YES;
    [SV removeFromSuperview];
    
}
-(void)cancel:(NSString *)cancel{
    [self closeKeyBoad];
}

-(void)shareBtn:(UIButton*)sender{
  
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"act" forKey:@"contentType"];
    [param setValue:[Dict objectForKey:@"actTypeCode"] forKey:@"contentId"];
    [param setValue:@"wx_single" forKey:@"shareTarget"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    
    [HttpRequest requestData:@"Company/getShareUrl" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
                [self.window makeKeyAndVisible];
                
                shareblackview=[[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
                shareblackview.backgroundColor=[UIColor blackColor];
                shareblackview.alpha=0.65;
                [self.window addSubview:shareblackview];
                
                SV = [[ShareView alloc]initWithFrame:CGRectMake(0, ScreenHeight-245, ScreenWidth,245)];
                SV.dataDict=severDataDict.dataDict;
                
                SV.mDelegate=self;
                [self.window addSubview:SV];
                
                UITapGestureRecognizer *singleFingerOne = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKeyBoad)];
                singleFingerOne.numberOfTouchesRequired = 1; //手指数
                singleFingerOne.numberOfTapsRequired = 1; //tap次数
                singleFingerOne.delegate = self;
                [shareblackview addGestureRecognizer:singleFingerOne];
                }else{
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
                
            }
            
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}


-(void)successfullyReturns:(NSString *)successfullyReturns{//分享成功后回调登记接口
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"tokenId"];
    NSMutableDictionary *param = [NSMutableDictionary new];
    [param setValue:Token forKey:@"tokenId"];
    [param setValue:@"act" forKey:@"contentType"];
    [param setValue:[Dict objectForKey:@"actTypeCode"] forKey:@"contentId"];
    [param setValue:@"wx_single" forKey:@"shareTarget"];
    [Global showIndeterminateHudSub:@"加载中，请稍后。。。" delay:30 view:self.view];
    [HttpRequest requestData:@"Company/ShareRecords/Add" params:param success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
            NSLog(@"====%@",severDataDict.dataDict);
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [Global hideProgressHUDInternal:self.view];
                [self closeKeyBoad];
                NewMediaPromotionViewController *loginVC=[[NewMediaPromotionViewController alloc] init];
                [self.navigationController pushViewController:loginVC animated:YES];
                
            }else{
                [Global hideProgressHUDInternal:self.view];
                [Global showTextHudSub:[severDataDict.dataDict objectForKey:@"ResultString"] view:self.view];
            }
        }
        else
        {
            [Global hideProgressHUDInternal:self.view];
            [Global showTextHudSub:severDataDict.resultStr view:self.view];
        }
        
    }];
}






- (void)address:(NSString *)addressStr{

    NSString *oreillyAddress =[NSString stringWithFormat:@"%@%@", [Dict objectForKey:@"actRegion"], [Dict objectForKey:@"actAddress"]];
    
    CLGeocoder *myGeocoder = [[CLGeocoder alloc] init];
    [myGeocoder geocodeAddressString:oreillyAddress completionHandler:^(NSArray *placemarks, NSError *error) {
        if ([placemarks count] > 0 && error == nil) {
            
            CLPlacemark *firstPlacemark = [placemarks objectAtIndex:0];
            double latitude=firstPlacemark.location.coordinate.longitude;
            NSString *latitudeString=[NSString stringWithFormat:@"%f",latitude];
            double longitude=firstPlacemark.location.coordinate.latitude;
            NSString *longitudeString=[NSString stringWithFormat:@"%f",longitude];
            NSLog(@"latitudeString= %@", longitudeString);
            NSLog(@"longitudeString= %@",latitudeString);
            NSMutableDictionary *dic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                        oreillyAddress,@"address",
                                        oreillyAddress,@"city",
                                        @"google",@"from_map_type",
                                        oreillyAddress,@"region", nil];
            
            [dic setValue:longitudeString forKey:@"google_lat"];
            [dic setValue:latitudeString forKey:@"google_lng"];
            
            NSLog(@"latitudeString= %@", longitudeString);
            NSLog(@"longitudeString= %@",latitudeString);
            
            
            MapViewControllers* h=[[MapViewControllers alloc] init];
            h.navDic=dic;
            h.mapType = RegionNavi;
            [self.navigationController pushViewController:h animated:YES];
            
            
            
        }
        else if ([placemarks count] == 0 && error == nil) {
            NSLog(@"Found no placemarks.");
        }
        else if (error != nil) {
            NSLog(@"An error occurred = %@", error); }
    }];
    


}

- (void)ACETelPrompt:(NSString *)ACETelPromptStr{
    
    [ACETelPrompt callPhoneNumber: [[Dict objectForKey:@"companyBaseInfo"] objectForKey:@"phone"]
                                     call:^(NSTimeInterval duration) {
                                         NSLog(@"User made a call of %.1f seconds", duration);
    
                                     } cancel:^{
                                         NSLog(@"User cancelled the call");
                                     }];
            
    

}









-(void)setNavi{
    [[self rdv_tabBarController] setTabBarHidden:YES];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    self.navigationController.interactivePopGestureRecognizer.enabled = YES;
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    
    UIButton*editor = [[UIButton alloc] initWithFrame:CGRectMake(0, 0.0, 30, 30)];
    [editor setImage:[UIImage imageNamed:@"fenxiang"] forState:UIControlStateNormal];
    [editor addTarget:self action:@selector(shareBtn:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* myBtnBtn = [[UIBarButtonItem alloc] initWithCustomView:editor];
    self.navigationItem.rightBarButtonItem = myBtnBtn;
    
    
    self.title = @"活动详情";
    self.view.backgroundColor=[UIColor whiteColor];
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     backColor,
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];
    
    
    
    
    
    
    
    
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}

- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}
@end